﻿using System.Xml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Marbles.Core.Web.Syndication.Atom
{
    public class AtomAuthor
    {

        public string Name { get { return this._name; } set { this._name = value; } }
        public string Email { get { return this._email; } set { this._email = value; } }
        public Uri Url { get { return this._url; } set { this._url = value; } }

        private Uri _url = null;
        private string _name = string.Empty;
        private string _email = string.Empty;

        public AtomAuthor(string Name, string Email, Uri Url)
        {
            this._url = Url;
            this._name = Name;
            this._email = Email;
        }

        public AtomAuthor()
        {
            this._url = null;
            this._name = string.Empty;
            this._email = string.Empty;
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<author>");
            sb.AppendLine(string.Format("<name>{0}</name>", FeedHelper.XmlEncode(this._name)));
            sb.AppendLine(string.Format("<email>{0}</email>", FeedHelper.XmlEncode(this._email)));
            if (this._url != null)
            {
                sb.AppendLine(string.Format("<uri>{0}</uri>", this._url.OriginalString));
            }
            else
            {
                sb.AppendLine("<uri></uri>");
            }

            sb.AppendLine("</author>");
            return sb.ToString();
        }

        public XmlNode ToXml(XmlDocument Document)
        {
            XmlNode node = Document.CreateNode(XmlNodeType.Element, "author", "http://www.w3.org/2005/Atom");
            node.AppendChild(FeedHelper.CreateElement(Document, "name", this._name, "http://www.w3.org/2005/Atom"));
            node.AppendChild(FeedHelper.CreateElement(Document, "email", this._email, "http://www.w3.org/2005/Atom"));
            if (this._url != null)
            {
                node.AppendChild(FeedHelper.CreateElement(Document, "uri", this._url.OriginalString, "http://www.w3.org/2005/Atom"));
            }
            else
            {
                node.AppendChild(FeedHelper.CreateElement(Document, "uri", string.Empty, "http://www.w3.org/2005/Atom"));
            }

            return node;
        }

        public XmlNode ToXml(XmlDocument Document, string TagName)
        {
            XmlNode node = Document.CreateNode(XmlNodeType.Element, TagName, "http://www.w3.org/2005/Atom");
            node.AppendChild(FeedHelper.CreateElement(Document, "name", this._name, "http://www.w3.org/2005/Atom"));
            node.AppendChild(FeedHelper.CreateElement(Document, "email", this._email, "http://www.w3.org/2005/Atom"));
            if (this._url != null)
            {
                node.AppendChild(FeedHelper.CreateElement(Document, "uri", this._url.OriginalString, "http://www.w3.org/2005/Atom"));
            }
            else
            {
                node.AppendChild(FeedHelper.CreateElement(Document, "uri", string.Empty, "http://www.w3.org/2005/Atom"));
            }

            return node;
        }
    }
}
